package bug.ws;

import bug.dao.api.ProjectDao;
import bug.dao.hibernate.ProjectDaoHibernate;
import bug.persistent.Project;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.SessionFactoryUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.jws.WebMethod;
import javax.jws.WebService;
import java.util.ArrayList;
import java.util.List;

@WebService(endpointInterface = "bug.ws.ProjectList")
public class ProjectListImpl implements ProjectList {

    private ProjectDao projectDao;

    public List<Project> list() {
        SessionFactory sf = ((ProjectDaoHibernate)projectDao).getSessionFactory();
        HibernateTemplate ht = ((ProjectDaoHibernate)projectDao).getHibernateTemplate();
        Transaction tx = sf.openSession().beginTransaction();

        List<Project> projects = projectDao.getAll();
        for (Project p : projects) {
            ht.initialize(p.getUserProjects());
            System.out.println(">>> WS: UserProjects size=" + p.getUserProjects().size());
        }

        tx.commit();
        return projects;
    }

//----------------------------------------------------------------

    public ProjectDao getProjectDao() {
        return projectDao;
    }

    public void setProjectDao(ProjectDao projectDao) {
        this.projectDao = projectDao;
    }
}
